package org.douxiaoban.client.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import org.douxiaoban.client.R;
import org.douxiaoban.client.api.MailData;
import org.douxiaoban.client.api.MiniBlogItemData;

import java.util.ArrayList;

public class MailAdapter extends BaseAdapter{
	private Context mContext;
	private ArrayList<MailData> mList;
	
	
	public MailAdapter(Context context){
		mContext = context;
		mList = new ArrayList<MailData>();
	}
	@Override
	public int getCount(){
		return mList.size(); 
	}
	
	@Override
	public Object getItem(int index){
		if(index < 0 || index > mList.size()){
			return null;
		}
		return mList.get(index);
	}
	
	public long getItemId(int index){
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup vGroup) {
		// TODO Auto-generated method stub
		if ( position > mList.size()){
			return view;
		} 
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		view = layoutInflater.inflate(R.layout.mail_item, null);
		TextView mailTitle = (TextView)view.findViewById(R.id.mailTitle);
		TextView mailSender = (TextView)view.findViewById(R.id.mailSender);
		TextView mailPublic = (TextView)view.findViewById(R.id.mailPublicTime);
		TextView mailFromOrTo = (TextView)view.findViewById(R.id.flag);
		
		MailData mailData = mList.get(position);
		
		if(mailData.mTitle.length() > 12){
			mailData.mTitle = mailData.mTitle.substring(0, 12);
			mailData.mTitle += "...";
		}
		mailTitle.setText(mailData.mTitle);
		
		if(mailData.mIsInbox){
			mailFromOrTo.setText("来自 >> ");
			mailSender.setText(mailData.mSenderName);
		}else{
			mailFromOrTo.setText("发往 >> ");
			mailSender.setText(mailData.mRecName);
		}
		if(mailData.mUnread){
			view.setBackgroundColor(Color.parseColor("#9FE2E3E3"));
		}
				mailPublic.setText(mailData.mPublicTime);
		return view;
	}
	
	public int getFirstId(){
		if(!mList.isEmpty()){
			MailData data = mList.get(0);
			return data.mId;	
		}
		return -1;
	}
	
	//获取最后一个的id值
	public int getLastId(){
		MailData data = mList.get(mList.size()-1);
		return data.mId;
	}
	
	//更多操作(往后插
	public void addDataTail(ArrayList<MailData> list){
		mList.addAll(list);
		this.notifyDataSetChanged();
	}
	
	public void addDataPre(ArrayList<MailData> list){
		mList.addAll(0, list);
		this.notifyDataSetChanged();
	}
	
	//删除某条豆油
	public void remove(int index){
		mList.remove(index);
		this.notifyDataSetChanged();
	}
	//刷新操作
	public void addDataToHead(ArrayList<MailData> list){
		mList.addAll(0, list);
		this.notifyDataSetChanged();
	}
}